//
//  GSIconPickerTextField.swift
//  BYLM
//
//  Created by lile on 15/9/8.
//  Copyright (c) 2015年 Gears Software. All rights reserved.
//

import UIKit

class GSIconPickerTextField: UITextField,UITableViewDataSource,UITableViewDelegate {
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
    // Drawing code
    }
    */
    
    var tableView:UITableView!
    var list:Array<TypeModel> =  Array<TypeModel>()
    var required = false
    var selectedIndex = 0
    override func awakeFromNib() {
        super.awakeFromNib()
        setup()
    }
    
    func setup(){
        
        
        let nib = NSBundle.mainBundle().loadNibNamed("ipfnib", owner: self, options: nil)
        let toolbar = nib[0] as! UIToolbar
        self.inputAccessoryView = toolbar
        var toolitems = toolbar.items as! Array<UIBarButtonItem>
        
        toolitems[1].action = "cancelSelected:"
        toolitems[2].action = "okSelected:"
        
        self.tableView = nib[1] as! UITableView
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.inputView = tableView
        
    }
    
    func cancelSelected(barButtonItem:UIBarButtonItem)
    {
        self.resignFirstResponder()
    }
    
    func okSelected(barButtonItem:UIBarButtonItem)
    {
        if list.count>0
        {
            self.text = list[selectedIndex].typeName
        }
        self.resignFirstResponder()
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list.count
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        selectedIndex = indexPath.row
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = NSBundle.mainBundle().loadNibNamed("ipfnib", owner: self, options: nil)[2] as! UITableViewCell
        let lbl = cell.viewWithTag(1) as! UILabel
     
        
        if list.count > 0
        {
           lbl.text = list[indexPath.row].typeName
        }
        
        return cell
    }
    
    func validate()->Bool
    {
        if required
        {
            if self.text != nil && self.text != ""
            {
                return true
            }
            else
            {
                return false
            }
        }
        else
        {
            return true
        }
    }
    
    override func textRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds,10, 5)
    }
    
    override func editingRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds,10, 5)

    }
    
    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
        if action == Selector("paste:")
        
        {
            return false
        }
        if action == Selector("select:")
        {
            return false
        }
        if action == Selector("selectAll:")
        {
            return false
        }
        
        return super.canPerformAction(action, withSender: sender)
    }
    
    
}
